package com.gzz.orchid.context.domain.rbac.entity;

import com.gzz.orchid.context.infrastructure.definition.Action;
import lombok.Data;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 权限
 *
 * @author higmd
 */
@Data
public class Permission {
    /**
     * 角色编码
     */
    private String roleCode;
    /**
     * 模块编码
     */
    private String moduleCode;
    /**
     * 权限动作
     */
    private Set<Action> actions;

    public static Permission of(String moduleCode, String roleCode, String[] actions) {
        Permission perm = new Permission();
        perm.setModuleCode(moduleCode);
        perm.setRoleCode(roleCode);
        perm.setActions(Arrays.stream(actions).map(Action::of).collect(Collectors.toSet()));
        return perm;
    }

    public static Permission of(String moduleCode, String roleCode, Integer actions) {
        Permission perm = new Permission();
        perm.setModuleCode(moduleCode);
        perm.setRoleCode(roleCode);
        perm.setActions(Action.parseSet(actions));
        return perm;
    }
}
